﻿using System;
using System.Collections.Generic;
using System.IO;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YJKRevitRoom.NumberRoomControler
{
	public class ShareParameterOperator
	{
		public static bool InsertSharedParameter(Autodesk.Revit.DB.Document doc, BuiltInCategory ost, string groupName, Dictionary<string, ParameterType> Parameters, bool visible)
		{
			bool result;
			try
			{
				if (doc == null || string.IsNullOrEmpty(groupName) || Parameters.Count <= 0)
				{
					result = false;
				}
				else
				{
					DefinitionFile definitionFile = ShareParameterOperator.SharedParametersFile(doc);
					if (definitionFile == null)
					{
						result = false;
					}
					else if (definitionFile.Groups == null)
					{
						result = false;
					}
					else
					{
						DefinitionGroup definitionGroup = definitionFile.Groups.get_Item(groupName);
						if (definitionGroup == null)
						{
							definitionGroup = definitionFile.Groups.Create(groupName);
						}
						foreach (KeyValuePair<string, ParameterType> keyValuePair in Parameters)
						{
							Definition definition = definitionGroup.Definitions.get_Item(keyValuePair.Key);
							if (definition == null)
							{
								definition = UnifiedModified.GetDefinition(definitionGroup, keyValuePair.Key, keyValuePair.Value, visible, true);
							}
							InstanceBinding instanceBinding = ShareParameterOperator.CreateInstanceBinding(doc, definition, ost);
							if (instanceBinding != null)
							{
								if (doc.ParameterBindings.Contains(definition))
								{
									doc.ParameterBindings.ReInsert(definition, instanceBinding);
								}
								else
								{
									doc.ParameterBindings.Insert(definition, instanceBinding);
								}
							}
						}
						result = true;
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		private static DefinitionFile SharedParametersFile(Autodesk.Revit.DB.Document doc)
		{
			doc.Application.SharedParametersFilename = Path.Combine(Product.DataLocation, "YArchitech Shared Parameters_CN.txt");
			return doc.Application.OpenSharedParameterFile();
		}

		private static InstanceBinding CreateInstanceBinding(Autodesk.Revit.DB.Document doc, Definition key, BuiltInCategory ost)
		{
			if (doc == null || key == null)
			{
				return null;
			}
			Category category = doc.Settings.Categories.get_Item(ost);
			if (category == null)
			{
				return null;
			}
			CategorySet categorySet = doc.Application.Create.NewCategorySet();
			BindingMap parameterBindings = doc.ParameterBindings;
			if (parameterBindings.Contains(key))
			{
                ElementBinding elementBinding = parameterBindings.get_Item(key) as ElementBinding;
				if (elementBinding.GetType() != typeof(InstanceBinding))
				{
					return null;
				}
				if (elementBinding.Categories.Contains(category))
				{
					return null;
				}
				foreach (object obj in elementBinding.Categories)
				{
					Category category2 = (Category)obj;
					categorySet.Insert(category2);
				}
			}
			categorySet.Insert(category);
			return doc.Application.Create.NewInstanceBinding(categorySet);
		}
	}
}
